package com.huangjue.backend.service.impl;

import com.huangjue.backend.entity.Course;
import com.huangjue.backend.entity.StudentCourse;
import com.huangjue.backend.mapper.CourseMapper;
import com.huangjue.backend.service.CourseService;
import com.huangjue.backend.dto.CourseWithTeacherDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import com.huangjue.backend.mapper.CourseCategoryMapper;
import javax.annotation.Resource;

@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Resource
    private CourseCategoryMapper courseCategoryMapper;

    @Override
    public List<Course> getAllCourses() {
        return courseMapper.selectAllCourses();
    }

    @Override
    public List<Course> getCoursesByKeywords(String keyword) {
        return courseMapper.getCoursesByKeywords(keyword);
    }

    @Override
    public List<Course> getRecommendCourse() {
        return courseMapper.getRecommendCourse();
    }

    @Override
    public List<CourseWithTeacherDTO> getAllCoursesWithTeacher() {
        return courseMapper.selectAllCoursesWithTeacher();
    }

    @Override
    public List<Course> getCoursesByTeacherId(Integer teacherId) {
        return courseMapper.selectCoursesByTeacherId(teacherId);
    }

    @Override
    public List<StudentCourse> getUserCourses(Integer userId) {
        return courseMapper.getUserCourses(userId);
    }

    @Override
    public boolean updateCourseViews(Integer courseId) {
        return courseMapper.updateCourseViews(courseId) > 0;
    }

    @Override
    public boolean updateCourseLikes(Integer courseId) {
        return courseMapper.updateCourseLikes(courseId) > 0;
    }

    @Override
    public List<Course> getCoursesByCategoryId(Integer categoryId) {
        List<Integer> ids = courseCategoryMapper.selectCourseIdsByCategoryId(categoryId);
        if (ids == null || ids.isEmpty()) {
            return java.util.Collections.emptyList();
        }
        return courseMapper.selectCoursesByIds(ids);
    }

    @Override
    public Course getCourseById(Integer courseId) {
        return courseMapper.selectById(courseId);
    }
} 